package com.zsk.shop.common.base;

import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.nio.charset.Charset;

/**
 * @author lv
 * @date 2020/3/9 18:37
 */
@Data
@ApiModel("全局返回构造")
public class BaseResponse<T> {
    private static final Logger logger = LoggerFactory.getLogger(BaseResponse.class);

    public static final Integer SUCCESS = 200;
    public static final Integer TOKEN_FAIL = 302;
    public static final Integer FAIL = 500;

    protected static BaseResponse SUCCESS_TIP = new BaseResponse("请求成功");

    @ApiModelProperty(name = "code", value = "返回状态码", dataType = "Integer")
    private Integer code = 200;
    @ApiModelProperty(name = "message", value = "消息", dataType = "String")
    private String message = "操作成功";
    @ApiModelProperty(name = "data", value = "数据", dataType = "T")
    private T data;
    @ApiModelProperty(name = "count", value = "数据数量", dataType = "Integer")
    private Integer count = 0;
    // 扩展信息
    @ApiModelProperty(name = "ext", value = "拓展", dataType = "Object")
    private Object ext;

    public Integer getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = Math.toIntExact(count);
    }

    public BaseResponse() {
    }

    public boolean isSuccess() {
        return this.getCode().equals(SUCCESS);
    }

    public BaseResponse(Integer code, Integer count, T data) {
        this.code = code;
        this.data = data;
        this.count = count;
    }

    public BaseResponse(Integer errCode, String errorMessage) {
        this.code = errCode;
        this.message = errorMessage;
    }

    public BaseResponse(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public BaseResponse(Integer code, String message, T data, Object ext) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.ext = ext;
    }

    public BaseResponse(Integer code, String message, T data, Object ext, Integer count) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.count = count;
        this.ext = ext;
    }

    public BaseResponse(T data) {
        this.code = SUCCESS;
        this.data = data;
        this.message = "成功";
    }

    public static BaseResponse success() {
        return new BaseResponse();
    }

    public static BaseResponse success(Object data) {
        return new BaseResponse(data);
    }

    public BaseResponse success(Integer code, String message, Object data) {
        return new BaseResponse(code, message, data);
    }

    public static BaseResponse success(String message, Object data) {
        return new BaseResponse(BaseResponse.SUCCESS, message, data);
    }

    public static BaseResponse success(String message, Object data, Integer count) {
        return new BaseResponse(BaseResponse.SUCCESS, message, data, null, count);
    }

    public static BaseResponse error() {
        return new BaseResponse(BizExceptionEnum.SERVER_ERROR.getCode(), BizExceptionEnum.SERVER_ERROR.getMessage());
    }

    public static BaseResponse error(String message) {
        return new BaseResponse(BizExceptionEnum.ERROR.getCode(), message);
    }

    public static BaseResponse error(String message, Object data, Object ext) {
        return new BaseResponse(BizExceptionEnum.ERROR.getCode(), message, data, ext);
    }

    public static BaseResponse error(String message, Object data) {
        return new BaseResponse(BizExceptionEnum.ERROR.getCode(), message, data);
    }

    public static BaseResponse error(String message, Integer code) {
        return new BaseResponse(code, message);
    }

    public static FastJsonConfig fastjsonConfig() {
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(new SerializerFeature[]{SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect});
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConfig.setCharset(Charset.forName("utf-8"));
        initOtherValueFilters(fastJsonConfig);
        return fastJsonConfig;
    }

    protected static void initOtherValueFilters(FastJsonConfig fastJsonConfig) {
        ValueFilter nullValueFilter = (object, name, value) -> {
            return value;
        };
        ValueFilter longValueFilter = (object, name, value) -> {
            if (value instanceof Long) {
                return String.valueOf(value);
            } else {
                return value instanceof BigInteger ? String.valueOf(value) : value;
            }
        };
        fastJsonConfig.setSerializeFilters(new SerializeFilter[]{nullValueFilter, longValueFilter});
    }
}
